﻿@using OpenIdProvider
@using MvcMiniProfiler

@using(MiniProfiler.Current.Step("Master.cshtml")){

var provider = new Uri(Current.RequestUri, Response.ApplyAppPathModifier("~/openid/provider"));
    
var title = (string)ViewData["Title"];
if(title.IsNullOrEmpty())
{
    title = (string)ViewData["PageTitle"];
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@title</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="@Html.CacheBreak("/Content/js/master.js")"></script>
    <link rel="stylesheet" type="text/css" href="@Html.CacheBreak("/Content/css/all.css")" />
    @if (Current.ShouldBustFrames)
    {
        <text>
            <script type="text/javascript">common.bustFrames();</script>

            @* Doesn't make sense for affiliate pages to advertise an end point, so only do it if we're frame busting *@
            <link rel="openid.server" href="@provider">
            <link rel="openid2.provider" href="@provider">
        </text>
        
        if (Current.LoggedInUser != null && Current.LoggedInUser.IsAdministrator)
        {
            @MvcMiniProfiler.MiniProfiler.RenderIncludes()
        }
    }
    else
    {
        // This implies we're in an affiliate iframe
        var backgroundOverriden = false;
        
        <text>
            @* Include styles from the affiliate *@
            @ViewData.IncludeThirdPartyStyles(out backgroundOverriden)
        </text>
        
        if (!backgroundOverriden)
        {
            <style type="text/css">
                body
                {
                    background: transparent !important;
                }
            </style>
        }

        @* Don't do this check if we're coming back from a POST, since the recovery path will just fail again if we need it a second time *@
        if (Request.HttpMethod != "POST")
        {
            <script type="text/javascript">affiliate.checkXSRF();</script>
        }
        else
        {
            var fallback = ViewData.ContainsKey("ThirdPartyCookieFallback") ? (string)ViewData["ThirdPartyCookieFallback"] : null;
            
            <script type="text/javascript">affiliate.failNoCanary('@Current.AppRootUri', '@(new HtmlString(fallback.HasValue()? fallback : ""))');</script>
        }
    }
    </head>
<body>
    @if (Current.ShouldBustFrames)
    {
        <div id="topbar">
            <div id="menubar">
                <div class="logocontainer">
                    <a id="logo" href="/"></a>
                </div>
                <div id="menu">
                    @if (Current.LoggedInUser == null)
                    {
                        <text>
                            @Html.RenderTab("Login", "/account/login")
                            @Html.RenderTab("Register", "/account/register")
                        </text>
                    }
                    else
                    {
                        <text>
                            @Html.RenderTab("Profile", "/user")
                            @Html.RenderTab("Change Password", "/account/password-reset")
                            @Html.RenderTab("Logout", "/account/logout")
                            @*
                            Commented out until we've got a UI for these
                            @Html.RenderTab("List Owned Affiliates", "/affiliate/list")
                            @Html.RenderTab("Register Affiliate", "/affiliate/register")
                            *@
                        </text>
                       
                        if (Current.LoggedInUser.IsAdministrator)
                        {
                            @Html.RenderTab("Admin Tools", "/admin")
                        }
                    }
                </div>
            </div>
        </div>
        
        <div id="content">
            <div id="mainbar">
                <h2 class="page-header">@ViewData["Title"]</h2>

                @using(MiniProfiler.Current.Step("RenderBody"))
                {
                    @RenderBody()
                }
            </div>

            <div id="footer">
                @Html.Raw(OpenIdProvider.Helpers.Template.FormatTemplate("Footer"))
            </div>

            @if (Current.LoggedInUser != null && Current.LoggedInUser.IsAdministrator)
            {
                <div id="web">
                    @Environment.MachineName &nbsp;
                    from &nbsp;
                    @Current.RemoteIP
                </div>
            }
        </div>
    }
    else
    {
        <div id="framed-content">
            <h2 class="page-header">@ViewData["Title"]</h2>

            @using(MiniProfiler.Current.Step("RenderBody"))
            {
                @RenderBody()
            }
        </div>
    }    
</body>
</html>
    
}